package cn.stylefeng.guns.modular.transport.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.transport.entity.TDispatchDriverEval;
import cn.stylefeng.guns.modular.transport.model.params.TDispatchDriverEvalParam;
import cn.stylefeng.guns.modular.transport.model.result.StoreJsonOV;
import cn.stylefeng.guns.modular.transport.model.result.TDispatchDriverEvalResult;
import cn.stylefeng.guns.modular.transport.service.TDispatchDriverEvalService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * 司机评价控制器
 *
 * @author zhangzh
 * @Date 2025-04-11 09:59:00
 */
@Controller
@RequestMapping("/tDispatchDriverEval")
public class TDispatchDriverEvalController extends BaseController {

    private String PREFIX = "/transport/tDispatchDriverEval";

    @Autowired
    private TDispatchDriverEvalService tDispatchDriverEvalService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2025-04-11
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/tDispatchDriverEval.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2025-04-11
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/tDispatchDriverEval_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2025-04-11
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/tDispatchDriverEval_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2025-04-11
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TDispatchDriverEvalParam tDispatchDriverEvalParam) {
        this.tDispatchDriverEvalService.add(tDispatchDriverEvalParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2025-04-11
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TDispatchDriverEvalParam tDispatchDriverEvalParam) {
        this.tDispatchDriverEvalService.update(tDispatchDriverEvalParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2025-04-11
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TDispatchDriverEvalParam tDispatchDriverEvalParam) {
        this.tDispatchDriverEvalService.delete(tDispatchDriverEvalParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2025-04-11
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TDispatchDriverEvalParam tDispatchDriverEvalParam) {
        TDispatchDriverEval detail = this.tDispatchDriverEvalService.getById(tDispatchDriverEvalParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2025-04-11
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TDispatchDriverEvalParam tDispatchDriverEvalParam,String condition ) {
        User user = userService.getcurrentUser();
        tDispatchDriverEvalParam.setDeptId(user.getDeptId());
        return this.tDispatchDriverEvalService.findPageBySpec(tDispatchDriverEvalParam,condition);
    }


    @ResponseBody
    @RequestMapping("/listMx")
    public LayuiPageInfo listMx(TDispatchDriverEvalParam tDispatchDriverEvalParam) {

        if(ToolUtil.isEmpty(tDispatchDriverEvalParam.getId())){
            return new LayuiPageInfo();
        }

        TDispatchDriverEvalParam q = new TDispatchDriverEvalParam();
        q.setId(tDispatchDriverEvalParam.getId());
        TDispatchDriverEvalResult tDispatchDriverEvalResult = this.tDispatchDriverEvalService.findBySpec(q);
        List<StoreJsonOV> storeList = new ArrayList<>();
        if(ToolUtil.isNotEmpty(tDispatchDriverEvalResult) && ToolUtil.isNotEmpty(tDispatchDriverEvalResult.getStoreJson())){
            storeList = JSONArray.parseArray(tDispatchDriverEvalResult.getStoreJson(),StoreJsonOV.class);
        }
        LayuiPageInfo LayuiPageInfo = new LayuiPageInfo();
        LayuiPageInfo.setData(storeList);
        LayuiPageInfo.setCount(storeList.size());
        return LayuiPageInfo;
    }




}


